﻿using System;
using System.Web.Security;
using System.Web.UI;
using Be;
using WebGui.Admin;
using WebGui.BllLessonService;
using WebGui.BllBookingService;

namespace WebGui.User {
    /// <summary>
    /// The Edit page
    /// </summary>
    public partial class Edit : Page {
        /// <summary>
        ///a Lesson service Client
        /// </summary>
        private LessonServiceClient _lesson;
        /// <summary>
        ///a Booking service Client
        /// </summary>
        private BookingServiceClient _booking;
        /// <summary>
        /// a object of BeBooking
        /// </summary>
        private BeBooking _event;
        /// <summary>
        /// loads the page
        /// </summary>
        /// <param name="sender">The sender of the event</param>
        /// <param name="e">The event Arguments</param>
        protected void Page_Load(object sender, EventArgs e) {
            try {
                _lesson = (LessonServiceClient) Session["Lesson"];
                _booking = (BookingServiceClient) Session["Booking"]; 
                if(!IsPostBack) {
                    int id = Convert.ToInt32(Request.QueryString["id"]);
                    loadBooking(id);
                    Session["userid"] = _event.UserId;
                    TxtStart.Text = _event.StartTime.ToString();
                    TxtEnd.Text = _event.EndTime.ToString();
                    TxtStartLad.Text = _event.StartLatitude +"";
                    TxtStartLog.Text = _event.StartLongitude +"";
                    TxtEndLad.Text = _event.EndLatitude + "";
                    TxtEndLog.Text = _event.EndLongitude + "";
                    DdlTecher.DataSource = Roles.GetUsersInRole("kørelære");
                    DdlTecher.DataBind();
                    
                }
            } catch(Exception ex) {
                Response.Write("<script type='text/javascript'>alert('" + ex.Message + "'); </script>");
            }
        }
        /// <summary>
        /// loads the booking 
        /// </summary>
        /// <param name="id">the booking id</param>
        private void loadBooking(int id)
        {
            try
            {
                _event = _booking.GetBookingFromId(id);
            }
            catch (Exception ex) 
            { Response.Write("<script type='text/javascript'>alert('" + ex.Message + "'); </script>"); }
        }
        /// <summary>
        /// Closes the page and modal
        /// </summary>
        /// <param name="sender">The sender of the event</param>
        /// <param name="e">The event Arguments</param>
        protected void BtnCancel_Click(object sender, EventArgs e) {
            Modal.Close(this);
        }
        /// <summary>
        /// Saves the edits to the database
        /// </summary>
        /// <param name="sender">The sender of the event</param>
        /// <param name="e">The event Arguments</param>
        protected void BtnOk_Click(object sender, EventArgs e) {
            try
            {
                int id = Convert.ToInt32(Request.QueryString["id"]);
               DateTime start = Convert.ToDateTime(TxtStart.Text);
                DateTime end = Convert.ToDateTime(TxtEnd.Text);
                double startlad = Convert.ToDouble(TxtStartLad.Text);
                double startlog = Convert.ToDouble(TxtStartLog.Text);
                double endlad = Convert.ToDouble(TxtEndLad.Text);
                double endlog = Convert.ToDouble(TxtEndLog.Text);
                string teacherId = Membership.GetUser(DdlTecher.SelectedValue).ProviderUserKey.ToString();
                BeBooking bebooking = _booking.GetBookingFromId(id);
                bebooking.TeacherId = teacherId;
                BeLesson lesson = _lesson.LessonPresents(bebooking);
                BeBooking booking = new BeBooking(bebooking.Id, start, end, startlad, startlog, endlad, endlog, bebooking.UserId, teacherId, lesson.Id);
                _booking.EditBooking(booking);
                Modal.Close(this, "OK");
            }
            catch (Exception ex) { Response.Write("<script type='text/javascript'>alert('" + ex.Message + "'); </script>"); }
        }
    }
}